#pragma once

#include "IManager.h"
#include "clientmessagemanager.h"
#include <Windows.h>


class ClientManager : public IManager
{
    Q_OBJECT

public:
    ClientManager(const QString& ip, int port, QGraphicsScene * scene);
    ~ClientManager();
    bool Connected();

public slots:
    void slotKeyPresEvent(QKeyEvent *event);
    void slotKeyReleaseEvent(QKeyEvent *event);

    // Call this method before constructing from snapshot
    virtual void slotStartSnapshot();

    // Call this method after constructing from snapshot
    virtual void slotFinishSnapshot();

    // Call this method to set tank configurations from snapshot
    virtual void slotSetTank(int tank_id, int health, int pos_x, int pos_y, char action, char direction, int progress);

    // Call this method to set bullet configurations from snapshot
    virtual void slotSetBullet(int bullet_id, int pos_x, int pos_y, char direction, int progress);

    // Call this method to set nickname of a player controlling this computer
    // param tank_id (OUT) - assigned id if non-negative, if nickname is not allowed then id is negative
    virtual void slotSetNickname(QString nick);

    virtual void slotDisconnect();

    virtual void slotRecievePlaylist(QStringList list);

    void slotUpdate();

    virtual void slotGameOver(int player_id);

    void slotConnectionSuccessfull();
    void slotStart();
    void slotFire(int tank_id);
    void slotStopFire(int tank_id);
    void slotMove(int tank_id);
    void slotTurn(int tank_id, char direction);
    void slotStopMove(int tank_id);

    void slotNicknameAccepted(int player_id);
    void slotNicknameRejected(QString message);

    // Message Manager calls this client's slot if the connection to host is successful
    virtual void slotHostConnected();
    virtual void slotHostFailedConnection();

signals:
    // These signals are for Message Manager
    void signalDisconnect();
    void signalFire();
    void signalStopFire();
    void signalMove();
    void signalTurn(char direction);
    void signalStopMove();
    void signalSendNickname(QString nick);

    // These signals are for Game Creator
    void signalHostConnected();
    void signalHostFailedConnection();
    void signalNicknameAccepted(int player_id);
    void signalNicknameRejected(QString message);
    void signalGameOver(int player_id);
    void signalPlaylist(QStringList list);
    void signalStartClientGame();


private:
    ClientMessageManager * m_ptr_messageManager;

    CRITICAL_SECTION m_cs_updating;
    CRITICAL_SECTION m_cs_command;

    bool connected;
};
